function genoption(data) {
    // the data has all the info we need
    return option = {
        title: {
            text: $('#sel1').val() + $('#sel2').val(),
            x: 'center'
        },
        tooltip: {
            trigger: 'item',
            formatter: "{c} ({d}%)"
        },
        legend: {
            orient: 'vertical',
            left: 'left',
            data: getLegend(data),
        },
        series: [
            {
                type: 'pie',
                radius: '55%',
                center: ['70%', '60%'],
                data: data,
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };
}


$(function () {
    const myCharts1 = echarts.init(document.getElementById('pic1'));
    $.post("get_analy", {'area': $('#sel1').val(), 'kind': $('#sel2').val()}, function (data) {
        data = JSON.parse(data);
        const option = genoption(data);
        myCharts1.setOption(option);
    });
    $('#sel1').change(function () {
        var val = $('#sel1').val();
        var val2 = $('#sel2').val();
        console.log(val);
        $.post("get_analy", {'area': val, 'kind': val2}, function (data) {
            data = JSON.parse(data);
            const option = genoption(data);
            myCharts1.setOption(option);
        });
    });
      $('#sel2').change(function () {
        var val = $('#sel1').val();
        var val2 = $('#sel2').val();
        console.log(val);
        $.post("get_analy", {'area': val, 'kind': val2}, function (data) {
            data = JSON.parse(data);
            const option = genoption(data);
            myCharts1.setOption(option);
        });
    });

});

function getLegend(data) {
    return data.map((item) => item.name, data)
}